<?php

namespace Beerus\Modules\Peanut\Actions;

use Encore\Admin\Actions\RowAction;
use Encore\Admin\Admin;
use Encore\Admin\Widgets\Form;

class EditBackStatus extends RowAction
{
    protected $uid;
    protected $back_ids;
    protected $type;

    public function __construct($uid, $back_ids, $type = 0)
    {
        $this->uid = $uid;
        $this->back_ids = $back_ids;
        $this->type = $type;
    }

    protected function script()
    {
        return <<<SCRIPT
$('.grid-check-edit-success').on('click', function () {
                            var This = $(this);
                            swal({
                                title: "确定要操作已选择的数据吗？",
                                type: "warning",
                                showCancelButton: true,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "确认",
                                showLoaderOnConfirm: true,
                                cancelButtonText: "取消",
                                preConfirm: function() {
                                //ajax
                                     $.ajax({
                                        method: 'post',
                                        url: '/admin/Peanut/Transfer/Operate',
                                        data: {
                                            _token: LA.token,
                                            uid: This.data('uid'),
                                            back_ids: This.data('back_ids'),
                                            type: This.data('type')
                                        },
                                        success: function (e) {
                                            if (e.code == 1) {
                                                swal({
                                                    title: e.title,
                                                    type: "success",
                                                });
                                                setTimeout(function () {
                                                    $.pjax.reload('#pjax-container');
                                                }, 1000);
                                            } else {
                                                toastr.error(e.title, null, {
                                                    timeOut : 2000
                                                }); 
                                            }
                                        }
                                    });
                              //ajax-end
                             }
            
                            });
                            
    // Your code.
    console.log($(this).data('uid'));
    
});

$('.grid-check-edit-fail').on('click', function () {
var This = $(this);
var inputOptions = new Promise(function(resolve) {
  resolve({
      '云账户信息有误': '云账户信息有误'
    });
});


swal({
  title: '转账失败原因',
  input: 'radio',
  showCancelButton: true,
  cancelButtonText: '取消',
  confirmButtonText: '提交',
  showLoaderOnConfirm: true,
  inputOptions: inputOptions,
  inputValue: "云账户信息有误",
  inputValidator: function(result) {
    console.log(result)
    if (result) {
        $.ajax({
        method: 'post',
        url: '/admin/Peanut/Transfer/Operate',
        data: {
            _token: LA.token,
            remark: result,
            uid: This.data('uid'),
            back_ids: This.data('back_ids'),
            type: This.data('type')
        },
        success: function (e) {
            if (e.code == 1) {
                swal({
                    title: e.title,
                    type: "success",
                });
                setTimeout(function () {
                    $.pjax.reload('#pjax-container');
                }, 1000);
            } else {
                toastr.error(e.title, null, {
                    timeOut : 2000
                }); 
            }
        }
        });
    } else {
       return new Promise(function(reject) {
        reject('请选择一个失败原因!');
       });
    }
  }
})


});

SCRIPT;
    }

    public function render()
    {
        Admin::script($this->script());

        if ($this->type == 1) {
            return "<a class='btn btn-xs btn-info fa grid-check-edit-success' data-uid='{$this->uid}' data-back_ids='{$this->back_ids}' data-type='{$this->type}'>待转账</a>";
        } elseif ($this->type == 2) {
            return "<a class='btn btn-xs btn-success fa grid-check-edit-success' data-uid='{$this->uid}' data-back_ids='{$this->back_ids}' data-type='{$this->type}'>转账成功</a>";
        } elseif ($this->type == 3) {
            return "<a class='btn btn-xs btn-danger fa grid-check-edit-fail' data-uid='{$this->uid}' data-back_ids='{$this->back_ids}' data-type='{$this->type}'>转账失败</a>";
        } else {
            return "<a class='hidden' id='transfer-edit-info' data-uid='{$this->uid}' data-back_ids='{$this->back_ids}' data-type='{$this->type}'></a>";
        }
    }

    public function __toString()
    {
        return $this->render();
    }

    public function html_fail()
    {
        $form = new Form();

        return $form->render();
    }
}